<?php

namespace app\admin\controller;

use app\admin\model\ProjectYearSwitch;
use app\common\controller\Backend;
use app\admin\model\Fields;
use app\admin\model\Admin;
use app\admin\model\AdminProject;
use think\Db;
use fast\Random;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Socialized extends Backend
{

    /**
     * Project模型对象
     * @var \app\admin\model\Project
     */
    protected $model = null;
    protected $fiveindustryArr = [];
    protected $streetArr = [];
    protected $industryCategoryArr = [];
    protected $tablename = 'project';
    protected $year;

    public function _initialize()
    {

        parent::_initialize();
        $this -> year = $this -> request -> get('year',date('Y'));
        $this->model = new \app\admin\model\Project([],$this -> year);
        // $this -> model -> table = 'nsig_project_2020';


        $groupIds = $this->auth->getGroupIds();
        $this->assignconfig('isSuperAdmin',$this->auth->isSuperAdmin());
        $this->assign('isSuperAdmin',$this->auth->isSuperAdmin());
        $this->assignconfig('uid',$this->auth->id);
        $this -> assign('year',$this -> year);
         $chargedep_data = $this->auth->getChargedep();
        if($chargedep_data)
            $chargedep_id = $chargedep_data->chargedep_id;
        else
            $chargedep_id = 0;

        $this->assignconfig('chargedep_id',$chargedep_id);

        if(in_array(GROUP_STREET,$groupIds) || in_array(GROUP_SOE,$groupIds))
        {
            $this->whereExtend[] = ['exp',Db::raw("FIND_IN_SET($chargedep_id,chargedep_ids)")];
        }
        $this->whereExtend['project_socialized'] = '是';

    }

    public function index() {
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model->with($this->relationtTable)
                ->where($where)
                ->where($this->whereExtend)
                ->order($sort, $order)
                ->count();

            $list = $this->model->with($this->relationtTable)
                ->where($where)
                ->where($this->whereExtend)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);
            // echo $this->model->getLastSql();
            return json($result);
        }
        return $this->view->fetch();
    }

    public function edit($ids = null){
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $result = false;
                $result = $row->allowField(true)->save($params);
                if ($result !== false)
                    $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $row->first_registration_images_arr = explode(",",$row->first_registration_images);
        $row->second_registration_images_arr = explode(",",$row->second_registration_images);
        $row->final_confirm_images_arr = explode(",",$row->final_confirm_images);
        $row->reward_application_images_arr = explode(",",$row->reward_application_images);

        $this->assign('row', $row);
        // pr($row->first_registration_images_arr);
        if($this->auth->isSuperAdmin())
            return $this->view->fetch('editadmin');
        else
            return $this->view->fetch();
    }
}